const app = getApp();
var url = app.data.url;
var picRoot = app.data.picRoot;
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchantId: '',
    show: false,
    roomBtn: 0,
    name:'',
    desc:'',
    num:'',
    price:'',
    discountPrice:'',
    payType: '0',
    uuid: '',
    putAwayStatus: '',
    unitList:[]
  },
  toShow: function (option) {
    let _that = this;
    let merchantId = _that.data.merchantId;
    wx.navigateTo({
      url: '/pages/show-recreation/index?merchantId=' + merchantId + "&descType=19&"
    })
  },
  /**
   * 删除
   */
  deleteUnit: function (option) {
    let _that = this;
    let item = option.currentTarget.dataset.item;
    let id = item.id;
    let uuid = _that.data.uuid;
    let merchantId = _that.data.merchantId;
    let title = item.title;
    Dialog.confirm({
      title: '提醒',
      message: '是否确定删除【' + title + "】"
    }).then(() => {
      wx.request({
        url: url + '/ScenicSystemApi/deleteTickets',
        data: {
          id: id,
          uuid: uuid
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.code == 200) {
            _that.setData({
              show: false
            })
            _that.queryData(merchantId);
            wx.showToast({
              title: "删除成功!",
              icon: "success",
              duration: 2000
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            })
          }
        }
      })
    }).catch(() => {
      // on cancel
    });

  },
  insertOrUpdate: function (option) {
    let _that = this;
    let name = _that.data.name;
    let desc = _that.data.desc;
    let merchantId = _that.data.merchantId;
    let num = _that.data.num;
    let itemDiscountPrice = _that.data.discountPrice;
    let itemPrice = _that.data.price;
    let payType = _that.data.payType;
    let uuid = _that.data.uuid;
    let putAwayStatus = _that.data.putAwayStatus;
    let method = option.currentTarget.dataset.method;
    let tempUrl;
    let tempData;
    if (method == 0) {
      tempUrl = url + '/ScenicSystemApi/insertScenicTickets';
      tempData = {
        title: name,
        description: desc,
        merchantId: merchantId,
        createUserName: uuid,
        updateUserName: uuid,
        originalPrice: itemPrice,
        discountPrice: itemDiscountPrice,
        payType: payType,
        putAwayStatus: putAwayStatus == true?0:1,
        num: num
      }
    } else {
      tempUrl = url + '/ScenicSystemApi/editScenicTickets';
      let id = _that.data.id;
      tempData = {
        id: id,
        title: name,
        description: desc,
        merchantId: merchantId,
        updateUserName: uuid,
        originalPrice: itemPrice,
        discountPrice: itemDiscountPrice,
        payType: payType,
        putAwayStatus: putAwayStatus == true ? 0 : 1,
        num: num
      }
    }
    wx.request({
      url: tempUrl,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          _that.setData({
            show: false
          })
          _that.queryData(merchantId);
          wx.showToast({
            title: '操作成功!',
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })

  },
  /**
  * 加载门票
  */
  queryData: function (merchantId) {
    let _that = this;
    console.log(merchantId);
    wx.request({
      url: url + '/ScenicSystemApi/listPage',
      data: {
        merchantId: merchantId,
        invoking: 1
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data);
          _that.setData({
            unitList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })
  },
  /**表单start */
  onNameChange: function(event){
    let _that = this;
    _that.setData({
      name: event.detail
    })
  },
  onDescChange: function (event) {
    let _that = this;
    _that.setData({
      desc: event.detail
    })
  },
  onNumChange: function (event) {
    let _that = this;
    _that.setData({
      num: event.detail
    })
  },
  onPriceChange: function (event) {
    let _that = this;
    _that.setData({
      price: event.detail
    })
  },
  onDiscountPriceChange: function (event) {
    let _that = this;
    _that.setData({
      discountPrice: event.detail
    })
  },
  onRadioChange: function (event) {
    let _that = this;
    _that.setData({
      payType: event.detail
    });
  },

  /**表单end */
  showPopup: function(option){
    console.log(option);
    let _that = this;
    let roomBtn = option.currentTarget.dataset.roombtn;
    let item = option.currentTarget.dataset.item;
    if(roomBtn == 1){
      // 回显
      let temp = item.putAwayStatus == 0?true:false;
      _that.setData({
        name: item.title,
        desc: item.description,
        num: item.num,
        price: item.originalPrice,
        discountPrice: item.discountPrice,
        payType: item.payType + '',
        putAwayStatus: temp,
        roomBtn: 1,
        id: item.id
      })
    }
    _that.setData({
      show: true
    })
  },
  onPutAwayStatusChange: function (event) {
    console.log(event);
    let _that = this;
    _that.setData({
      putAwayStatus: event.detail
    });
  },
  closePopup() {
    let _that = this;
    _that.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let merchantId = options.merchantId;
    // let merchantId = 38;
    let uuid = app.globalData.uuid
    let _that = this;
    _that.setData({
      merchantId: merchantId,
      uuid: uuid
    })
    _that.queryData(merchantId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})